// 导出的 时候，是 connections.promise()  所以下面不需要写 .promise().execute()
const connection = require('../app/database')
class UserService {
  // 创建用户
  async create(user) {
    const { name, password } = user
    // 将 user 存储到 数据库中
    const statement = `INSERT INTO user (name,password) VALUES (?,?);`
    const result = await connection.execute(statement, [name, password])
    console.log('用户注册到数据库中~');
    return result
  }

  // 查看用户是否已经被注册
  async getUserByName(name) {
    const statement = `SELECT * FROM user WHERE name = ?;`;
    const result = await connection.execute(statement, [name]);
    return result[0];
  }
}
module.exports = new UserService()